package pl.edu.agh.mr.nb.server;

import java.util.ArrayList;

public class NodeArray {

	ArrayList<Node> workingList; 
	ArrayList<Node> idleList;
	
	public NodeArray() {
		workingList = new ArrayList<Node>();
		idleList = new ArrayList<Node>();
	}

	//adding node to the idle list 
	public void registerNode(Node node) {
		idleList.add(node);
	}

	// removing node from the idle list 
	public void unregisterNode(String nodeName) {
		for (int i=0; i<idleList.size();i++)
			if (idleList.get(i).nodeName.equals(nodeName)){
				workingList.add(idleList.get(i));
				idleList.remove(i);
				i = idleList.size();
			}
	}

	// get an idleNode
	public Node getIdleNode() {
		
		if (idleList.size() == 0)
			return null;
		
		Node temp = idleList.remove(0);
		workingList.add(temp);
		
		return temp;
	}

	public Node getNodeByName(String nodeName) {
		
		for (int i=0; i<workingList.size();i++)
			if (workingList.get(i).nodeName.equals(nodeName)){
				workingList.get(i).nodeStatus = NodeStatus.IDLE;
				return workingList.remove(i);
			}
		
		return null;
	}

	public void monitor() {
		System.out.println("Working nodes");
		for (int i=0; i<workingList.size();i++){
			System.out.print(workingList.get(i).nodeName+",");
		}

		System.out.println("\nIdle nodes");
		for (int i=0; i<idleList.size();i++){
			System.out.print(idleList.get(i).nodeName+",");
		}

	}
}
